<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="ctx" scope="page" value="${request.contextPath}" />
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE  >
<html >
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,viewport-fit=cover">
    <title>报修历史</title>
    <link rel="stylesheet" type="text/css" href="${ctx}/static/weui/1.1.2/weui-for-work.min.css"/>
    <script type="text/javascript" src="${ctx}/static/jquery/jquery-1.8.3.min.js"></script>
    <script type="text/javascript" src="${ctx}/static/weui/1.1.2/weui.min.js"></script>
    <script type="text/javascript" src="${ctx}/static/weixin/wx_common.js"></script>
    <!-- 企业微信的JS-SDK -->
    <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
    <style type="text/css">
        .preview-img{
            width: 100%;
            height: 100%;
            position: absolute;
            top: 0;
            background-color: #000000;
            z-index: 1000;
        }
        .preview-img img{
            top: 50%;
            left: 50%;
            position: absolute;
            transform: translate(-50%,-50%);
        }
    </style>
</head>
<body style="background-color: #f8f8f8" >
<div class="page">
<div class="page__bd">
    <input type="hidden" name="userId" id="userId" value="${userId}"/>
<c:forEach items="${repaireInfos}" var="repaireInfo">
    <div class="weui-form-preview">
        <div class="weui-form-preview__bd">
            <div class="weui-form-preview__item">
                <label class="weui-form-preview__label">报修人</label>
                <span class="weui-form-preview__value"><c:out value="${repaireInfo.creatorName}"/></span>
            </div>
            <%--<div class="weui-form-preview__item">
                <label class="weui-form-preview__label">公司名</label>
                <span class="weui-form-preview__value"><c:out value="${repaireInfo.callerCompanyName}"/></span>
            </div>--%>
            <div class="weui-form-preview__item">
                <label class="weui-form-preview__label">部门</label>
                <span class="weui-form-preview__value"><c:out value="${repaireInfo.callerDeptName}"/></span>
            </div>
            <div class="weui-form-preview__item">
                <label class="weui-form-preview__label">地址</label>
                <span class="weui-form-preview__value"><c:out value="${repaireInfo.address}"/></span>
            </div>
            <div class="weui-form-preview__item">
                <label class="weui-form-preview__label">选项</label>
                <span class="weui-form-preview__value"><c:out value="${repaireInfo.machineType}"/></span>
            </div>
            <div class="weui-form-preview__item">
                <label class="weui-form-preview__label">判断常用</label>
                <span class="weui-form-preview__value"><c:out value="${repaireInfo.repaireSubject}"/></span>
            </div>
            <div class="weui-form-preview__item">
                <label class="weui-form-preview__label">备注</label>
                <span class="weui-form-preview__value"><c:out value="${repaireInfo.description}"/></span>
            </div>
            <div class="weui-form-preview__item">
                <label class="weui-form-preview__label">创建时间</label>
                <span class="weui-form-preview__value"><fmt:formatDate pattern="yyyy-MM-dd HH:mm" value="${repaireInfo.createTime}" type="both"/></span>
            </div>
            <c:if test="${ not empty repaireInfo.picAddress}">
            <div class="weui-form-preview__item">
                <label class="weui-form-preview__label">照片</label>
                <span class="weui-form-preview__value"><img src="/pic/getPic?fileId=${repaireInfo.picAddress}" style="width:50px;" onclick="imagePreview('${repaireInfo.picAddress}')"/> </span>
            </div>
            </c:if>
            <c:if test="${repaireInfo.status=='已接单'}">
                <div class="weui-form-preview__item">
                    <label class="weui-form-preview__label">服务时间</label>
                    <span class="weui-form-preview__value"><c:out value="${repaireInfo.expectTime}"/></span>
                </div>
            </c:if>
        </div>
        <div class="weui-form-preview__ft">
            <c:if test="${(repaireInfo.status=='待接单' || repaireInfo.status=='待调配')}">
                <a class="weui-form-preview__btn weui-form-preview__btn_default" href="javascript:allocationEngineer(${repaireInfo.id})">转单</a>
                <button type="submit" class="weui-form-preview__btn weui-form-preview__btn_primary" onclick="javascript:acceptRepaire('${repaireInfo.id}','${repaireInfo.createTime}')">接单</button>

            </c:if>
            <c:if test="${repaireInfo.status=='已接单'}">
                <a class="weui-form-preview__btn weui-form-preview__btn_primary" href="javascript:beginServer(${repaireInfo.id})"><font color="blue">开始服务</font></a>
                <button type="submit" class="weui-form-preview__btn weui-form-preview__btn_primary" disabled onclick="javascript:endServer('${repaireInfo.id}','${repaireInfo.status}')">结束服务</button>
            </c:if>
            <c:if test="${repaireInfo.status=='服务中'}">
                <a class="weui-form-preview__btn weui-form-preview__btn_default" href="javascript:repaireDetail(${repaireInfo.id})">详情</a>
                <button type="submit" class="weui-form-preview__btn weui-form-preview__btn_primary" onclick="javascript:endServer('${repaireInfo.id}','${repaireInfo.status}')">结束服务</button>
            </c:if>
        </div>
    </div>
    <br>
</c:forEach>
</div>
    <c:if test="${empty repaireInfos}">

        <div class="weui-msg">
            <div class="weui-msg__icon-area"><i class="weui-icon-waiting weui-icon_msg"></i></div>
            <div class="weui-msg__text-area">
                <h2 class="weui-msg__title">暂时没有工单了</h2>
            </div>
        </div>
    </c:if>
</div>

<div id="toast" style="display: none;">
    <div class="weui-mask_transparent"></div>
    <div class="weui-toast">
        <i class="weui-icon-warn weui-icon_toast"></i>
        <p class="weui-toast__content"></p>
    </div>
</div>

<div id="toast_success" style="display: none;">
    <div class="weui-mask_transparent"></div>
    <div class="weui-toast">
        <i class="weui-icon-success-no-circle weui-icon_toast"></i>
        <p class="weui-toast__content"></p>
    </div>
</div>
<div class="preview-img" hidden onclick="$(this).hide()">
    <img src="" />
</div>
<script type="text/javascript" src="${ctx}/static/weixin/allocation_repaire.js" ></script>
<script>
    $(function () {

        popState();

        var needRefresh = sessionStorage.getItem("need-refresh");
        if(needRefresh){
            sessionStorage.removeItem("need-refresh");
            location.reload();
        }
    });
    var checkSubmitFlg = true;
    function dateToGMT(strDate){
        var dateStr=strDate.split(" ");
        var strGMT = dateStr[0]+" "+dateStr[1]+" "+dateStr[2]+" "+dateStr[5]+" "+dateStr[3]+" GMT+0800";
        var date = new Date(Date.parse(strGMT));
        return date;
    }
    var ctx = "${ctx}";
    function imagePreview(fileId) {
//        if(window.WeixinJSBridge) {
//            WeixinJSBridge.invoke("imagePreview",{
//                'current': ctx + "/pic/getPic?fileId=" + fileId,
//                'urls': ctx + "/pic/getPic?fileId=" + fileId
//            });
//        }
        $(".preview-img").show();
        $(".preview-img").find("img").attr("src",ctx + "/pic/getPic?fileId=" + fileId);

    }
    function afterWorkTime(id,begin_times,end_times) {
        var begin_time = {};
        begin_time.label = '上午';
        begin_time.value  = '上午';
        begin_times.push(begin_time);
        var begin_time = {};
        begin_time.label = '下午';
        begin_time.value  = '下午';
        begin_times.push(begin_time);
        weui.picker(begin_times, {
            defaultValue: ['上午'],
            onConfirm: function(result) {
                var time = result[0].value;
                location.href = '${ctx}/weixin/engineer/acceptRepaire/'+id+"?time="+time;
            }
        });
    }
    function beforeWorkTime(id,createTime,begin_times,end_times) {
        var now = new Date();
        var minutes=now.getMinutes()>30?"30":"00";
        var hours=now.getHours();
        for(var i=hours;i<=17;i+=0.5){
            if(i == hours && minutes=="30")
                continue;
            var time = {};
            if((i+'').endWith('.5')){
                time.label = (i-0.5)+':30  -  '+(i+0.5)+':00';
                time.value  = (i-0.5)+':30  -  '+(i+0.5)+':00';
            }else{
                time.label = i+':00  -  '+i+':30';
                time.value  = i+':00  -  '+i+':30';
            }
            begin_times.push(time);
        }
        var defaultHours= hours+":"+minutes+"  -  "+(minutes=="00"?hours+":30":(hours+1)+":00");
        if(begin_times.length==0){
            begin_times.push(defaultHours);
        }
        weui.picker(begin_times, {
            defaultValue: [defaultHours],
//                    onChange: function (result) {
//                        result[0].label=result[2].label;
//                        result[0].value=result[2].label;
//                        $(".weui-picker__content").eq(2).attr("style",$(".weui-picker__content").eq(0).attr("style"));
//                        console.log($(".weui-picker__content").eq(0).attr("style"));
//                    },
            onConfirm: function(result) {
//                        var time = result[0].label + '-' + result[2].label;
//                    var expect_date = date + ' ' + time;
//                    $(_this).find('.weui-cell__ft').text(expect_date);
                location.href = '${ctx}/weixin/engineer/acceptRepaire/'+id+"?time="+result[0].label;
            }
        });
    }
    function acceptRepaire(id,createTime) {
        createTime = dateToGMT(createTime);
        var date = new Date();
        date.setHours(17);
        date.setMinutes(30);
        var begin_times = [],
            end_times = [],
            symbol = [{ label: '-', value: 0 }];
        if(date.getDate() == createTime.getDate()){
            // 创建时间晚于17:30 或者当前时间晚于17.30   都按照第二天处理
            if(createTime.getTime() - date.getTime()>0 || new Date().getTime()-date.getTime()>0){
                afterWorkTime(id,begin_times,end_times);
            }else { // 早于17::30  半小时工作量
                beforeWorkTime(id,createTime,begin_times,end_times);
            }
        }else{
            afterWorkTime(id,begin_times,end_times);
        }

    }
    // 开始服务
    function beginServer(id) {
        if (checkSubmitFlg == true) {
            checkSubmitFlg = false;
            location.href = '${ctx}/weixin/engineer/beginServer/'+id;
        }
    }
    function repaireDetail(id) {
        if (checkSubmitFlg == true) {
            checkSubmitFlg = false;
            location.href = '${ctx}/weixin/engineer/repaireDetail/'+id;
        }
    }
    // 结束服务
    function endServer(id,status) {
        if(status =='服务中'){
            if (checkSubmitFlg == true) {
                checkSubmitFlg = false;
                location.href = '${ctx}/weixin/engineer/endServer/'+id;
            }
        }
    }

    function allocationEngineer(id){
        if (checkSubmitFlg == true) {
            getEngineer(id,'${ctx}/weixin/engineer/list?userId='+$('#userId').val());
        }
    }

    // 选择工程师派单
    function renderSelect(data,id,url) {
        weui.picker(data, {
            onChange: function (result) {
            },
            onConfirm: function (result) {
                var currentId = result[0].value;
                var currentName = result[0].label;
                var json = {"id":id,"currentId":currentId,"currentName":currentName};
                update(json,function () {
                    showSuccessToast('派单成功');
                    location.href= url;

                })
            }
        });
    }


        String.prototype.endWith=function(str){
            if(str==null||str==""||this.length==0||str.length>this.length)
                return false;
            if(this.substring(this.length-str.length)==str)
                return true;
            else
                return false;
            return true;
        }

        String.prototype.startWith=function(str){
            if(str==null||str==""||this.length==0||str.length>this.length)
                return false;
            if(this.substr(0,str.length)==str)
                return true;
            else
                return false;
            return true;
        }
</script>
</body>
</html>
